package Chapter6.dao;

import java.util.HashSet;
import java.util.Set;

import learnning.dao.base.HibernateDao;

import org.hibernate.HibernateException;

import Chapter6.many_to_many.School;
import Chapter6.many_to_many.Teacher;

public class SchoolDao extends HibernateDao {
	public void add(String schoolName, String teacherName, String address) {
		try {
			begin();
			Teacher teacher = new Teacher(teacherName);
			Teacher teacher2 = new Teacher(teacherName);
			School school = new School(schoolName, address);
			getSession().save(school);
			Set<Teacher> teachers = new HashSet<Teacher>();
			teachers.add(teacher);
			teachers.add(teacher2);
			school.setTeachers(teachers);
			Set<School> schools = new HashSet<School>();
			schools.add(school);
			teacher.setSchools(schools);
			teacher2.setSchools(schools);
			getSession().save(teacher);
			getSession().save(teacher2);
			commit();
		} catch (HibernateException e) {
			rollback();
			e.printStackTrace();
		}
	}
}
